import Api from "@/api";
import Vue from "vue";

export default class Auth extends Vue {
  protected errmsg = "";
  protected uuid = "";
  // 账号
  protected username = "";

  // 密码
  protected password = "";

  // 手机号
  protected mobile = "";

  // 验证码
  protected code = "";

  /**
   * 账号密码登陆
   */
  async UsernameLogin(params: Api.Login) {
    const res: Api.Response = await Api.Login(params);
    if (res && res.code === 200) {
      localStorage.setItem("token", res.token);
      return await this.GetInfo();
    } else {
      this.errmsg = res.msg;
      return Promise.resolve(res);
    }
  }
  /**
   * 手机号登陆
   */
  async MobileLogin(params: Api.PhonenumberLogin) {
    const res: Api.Response = await Api.PhonenumberLogin(params);
    if (res && res.code === 200) {
      localStorage.setItem("token", res.token);
      return await this.GetInfo();
    } else {
      this.errmsg = res.msg;
      return Promise.resolve(res);
    }
  }
  /**
   * 发送验证码
   */
  async CaptchaSMS(params: Api.CaptchaSMS) {
    const res: Api.Response = await Api.CaptchaSMS(params);
    if (res && res.code === 200) {
      return res;
    } else {
      return Promise.resolve(res);
    }
  }

  /**
   * 获取用户信息
   */
  async GetInfo() {
    const res: Api.Response = await Api.GetInfo();
    if (res && res.code === 200) {
      return res;
    } else {
      return Promise.resolve(res);
    }
  }

  /**
   * 清楚登陆信息
   */
  Clear() {
    localStorage.removeItem("token");
  }
}
